package com.lc.soft.utils;

import com.lc.soft.entity.DistrictEntity;
import com.lc.soft.entity.DistrictNestingEntity;
import com.lc.soft.entity.MapEntity;
import lombok.extern.log4j.Log4j2;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

@Log4j2
public class CommonUtils {

    public static MapEntity createMapEntity(MapEntity mapEntity) {
        try {
            Double maxLng = null, minLng = null, maxLat = null, minLat = null;
            JSONObject jsonObject1 = JSONObject.fromObject(mapEntity.getGeojson());
            JSONArray jsonArray1 = jsonObject1.getJSONArray("features");
            JSONObject jsonObject2, jsonObject3;
            JSONArray jsonArray2, jsonArray3, jsonArray4, jsonArray5;
            double lng, lat;
            for (Object object1 : jsonArray1) {
                jsonObject2 = JSONObject.fromObject(object1);
                jsonObject3 = jsonObject2.getJSONObject("geometry");
                jsonArray2 = jsonObject3.getJSONArray("coordinates");
                jsonArray3 = jsonArray2.getJSONArray(0);
                jsonArray4 = jsonArray3.getJSONArray(0);
                for (Object object2 : jsonArray4) {
                    jsonArray5 = JSONArray.fromObject(object2);
                    lng = jsonArray5.getDouble(0);
                    lat = jsonArray5.getDouble(1);
                    if (maxLng == null || maxLng < lng) {
                        maxLng = lng;
                    }
                    if (minLng == null || minLng > lng) {
                        minLng = lng;
                    }
                    if (maxLat == null || maxLat < lat) {
                        maxLat = lat;
                    }
                    if (minLat == null || minLat > lat) {
                        minLat = lat;
                    }
                }
            }
            if (maxLng == null) {
                return null;
            }
            double centerLng = (maxLng + minLng) / 2;
            double centerLat = (maxLat + minLat) / 2;
            mapEntity.setMaxLng(maxLng);
            mapEntity.setMinLng(minLng);
            mapEntity.setMaxLat(maxLat);
            mapEntity.setMinLat(minLat);
            mapEntity.setCenterLng(centerLng);
            mapEntity.setCenterLat(centerLat);
            return mapEntity;
        } catch (Exception e) {
            log.error(e);
            return null;
        }
    }

    public static List<DistrictNestingEntity> getDistrictTree(List<DistrictEntity> list) {
        List<DistrictNestingEntity> newList = new ArrayList<>();
        for (DistrictEntity districtEntity : list) {
            newList.add(new DistrictNestingEntity(districtEntity));
        }
        List<DistrictNestingEntity> districtNestingEntityList = new ArrayList<>();
        List<DistrictNestingEntity> children;
        for (DistrictNestingEntity districtNestingEntity1 : newList) {
            if (districtNestingEntity1.getParentId() == 0) {
                districtNestingEntityList.add(districtNestingEntity1);
            } else {
                for (DistrictNestingEntity districtNestingEntity2 : newList) {
                    if (districtNestingEntity1.getParentId() == districtNestingEntity2.getId()) {
                        children = districtNestingEntity2.getChildren();
                        if (children == null) {
                            children = new ArrayList<>();
                        }
                        children.add(districtNestingEntity1);
                        districtNestingEntity2.setChildren(children);
                        break;
                    }
                }
            }
        }
        return districtNestingEntityList;
    }
}
